package client.comm.baoding.ui.vm

import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import client.comm.baoding.api.bean.BankBind
import client.comm.baoding.api.bean.PayAccount
import client.comm.baoding.ui.viewmodel.MiddleVm
import client.comm.commlib.network.data.JsonResult
import client.comm.commlib.network.data.Ret
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.Util
import okhttp3.internal.notify

class SetBank3ViewModel(app: Application) : MiddleVm(app) {
    val isShowing = MutableLiveData<Boolean>(false)

    val tel_no = MutableLiveData<String>()
    val bank_name = MutableLiveData<String>()
    val card_id = MutableLiveData<String>()

    val vip_code = MutableLiveData<String>("")
    val expiration = MutableLiveData<String>("")
    val c_type = MutableLiveData<Int>(-1)


    val smscodeThree = MutableLiveData<String>()
    val getCodeTxt = MutableLiveData<String>("获取验证码")
    val getCodeEnable = MutableLiveData<Boolean>(true)

    val callBind = MutableLiveData<JsonResult<BankBind>>()
    val codeRet = MutableLiveData<JsonResult<Ret>>()

    val callBindCode = MutableLiveData<JsonResult<Ret>>()
    val confirmPay = MutableLiveData<JsonResult<Ret>>()
    val dataOne = MutableLiveData<JsonResult<PayAccount>>()

//    fun callOne() {
//        hold({
//            server.user_pay_account(1)
//        }, success = {
//            dataOne.value = it
//        })
//    }

//    var checkType: ((type: Int) -> Unit)? = null


    fun bindCard() {
        if (tel_no.value.isNullOrEmpty() || card_id.value.isNullOrEmpty()|| bank_name.value.isNullOrEmpty()) {
            toast("所有不得为空")
            return
        }
//        if (c_type.value == -1) {
//            toast("请选择银行卡类型")
//            return
//        }
//        when (c_type.value) {
//            0 -> {
//
//            }
//            1 -> {
//                if (vip_code.value.isNullOrEmpty() || expiration.value.isNullOrEmpty()) {
//                    toast("所有不得为空")
//                    return
//                }
//            }
//        }
        hold({
            server.bind_card(
                tel_no.value!!.trim(),
                card_id.value!!.replace(" ", ""),
                bank_name.value!!,
//                "${c_type.value}", vip_code.value!!,
//                expiration.value!!
            )
        }, {
            isShowing.value = true
        }, {
            callBind.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun getCode() {
        if (TextUtils.isEmpty(tel_no.value)) {
            toast("手机号不能为空")
            return
        }

        isShowing.value = true
        hold({
            val ts = System.currentTimeMillis()
            server.getcode(
                tel_no.value!!,
                "${ts}",
                Util.getCaptchaSign(tel_no.value!!, "${ts}")
            )
        }, {
            getCodeEnable.value = false
            codeRet.value = null
        }, {
            codeRet.value = it
        }, complete = {
            isShowing.value = false
        }, err = {
            getCodeEnable.value = true
        })

    }

    fun verifyPhone(bank_bind: BankBind, status: String) {
        if (smscodeThree.value.isNullOrEmpty()) {
            toast("请输入验证码")
            return
        }
        hold({
            server.sms_code_confirm(
                smscodeThree.value!!,
                bank_bind.auth_sn!!,
                bank_bind.request_no!!,
                status
            )
        }, {
            isShowing.value = true
        }, {
            codeRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

//    fun confirmPay(bank_bind: BankBind) {
//        if (smscodeThree.value.isNullOrEmpty()) {
//            toast("请输入验证码")
//            return
//        }
//        hold({
//            server.confirm_pay(
//                smscodeThree.value!!,
//                bank_bind.auth_sn,
//                bank_bind.request_no,
//            )
//        }, {
//            isShowing.value = true
//        }, {
//            codeRet.value = it
//        }, complete = {
//            isShowing.value = false
//        })
//    }


}